package com.sky.financelake.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.appcompat.widget.Toolbar;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.R;
import com.sky.financelake.adapter.CombinationAdapter;
import com.sky.financelake.base.BaseToolA;
import com.sky.financelake.bean.db.Combination;
import com.sky.financelake.common.Constants;
import com.sky.financelake.databinding.ActivityTagSelectToolBinding;
import com.sky.financelake.enums.StockToolEnums;
import com.sky.financelake.ui.wigets.FlowTagLayout;
import com.sky.financelake.viewmodels.local.CombinationViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class TagSelectToolA extends BaseToolA {

    private ActivityTagSelectToolBinding binding;
    private final List<String> selectTags = new ArrayList<>();
    private final List<String> optionalTags = new ArrayList<>();
    private List<String> combinations = new ArrayList<>();
    private CombinationAdapter adapter;
    private CombinationViewModel combinationViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityTagSelectToolBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        combinationViewModel = new ViewModelProvider(this).get(CombinationViewModel.class);

        Toolbar toolbar = binding.toolbarContainer.toolbar;
        initToolbar(toolbar, "标签选择工具");

        FlowTagLayout ftlSelect = binding.ftlSelect;
        FlowTagLayout ftlOptional = binding.ftlOptional;
        initTags();
        ftlOptional.addTags(optionalTags);


        ftlOptional.setTagClickListener(new FlowTagLayout.OnTagClickListener() {
            @Override
            public void tagClick(int position) {
                String tag = optionalTags.get(position);
                selectTags.add(tag);
                ftlSelect.addTag(tag);
            }
        });

        ftlSelect.setTagLongClickListener(new FlowTagLayout.OnTagLongClickListener() {
            @Override
            public void tagLongClick(int position) {
                selectTags.remove(position);
                ftlSelect.removeTagOfIndex(position);
                Log.i(TAG, "tagLongClick: " + position);
            }
        });

        RecyclerView rvCombination = binding.rvCombination;
        rvCombination.setLayoutManager(new LinearLayoutManager(this));
        adapter = new CombinationAdapter(combinations, this);
        adapter.setOnItemClickListener(new CombinationAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                Intent intent = new Intent(TagSelectToolA.this, CustomSelectToolA.class);
                intent.putExtra(Constants.COMBINATION, combinations.get(position));
                startActivity(intent);
            }
        });
        rvCombination.setAdapter(adapter);
        combinationViewModel.getCombinationsLiveData(this).observe(this, new Observer<List<String>>() {
            @Override
            public void onChanged(List<String> strings) {
                combinations.addAll(strings);
                adapter.notifyDataSetChanged();
            }
        });

        combinationViewModel.getCombinations(this);

        binding.tvSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectTags.size() > 0) {
                    StringBuilder sb = new StringBuilder();
                    StringBuilder sb_value = new StringBuilder();
                    for (String str : selectTags) {
                        for (StockToolEnums e : StockToolEnums.values()) {
                            if (str.equals(e.getName())){
                                Log.i(TAG, "onClick: "+e.name());
                                sb.append(e.name() + ",");
                                sb_value.append(e.name() + ",");
                            }
                        }
                    }
                    try {
                        combinationViewModel.insertCombinations(TagSelectToolA.this, new Combination(sb.toString()));
                    } catch (Exception e) {
                        combinationViewModel.displayShortSnackBar(TagSelectToolA.this, binding.tvSearch, getString(R.string.save_combination_fail));
                    }
                    Intent intent = new Intent(TagSelectToolA.this, CustomSelectToolA.class);
                    intent.putExtra(Constants.COMBINATION, sb.toString());
                    startActivity(intent);
                } else {
                    combinationViewModel.displayShortSnackBar(TagSelectToolA.this, binding.tvSearch, getString(R.string.no_select_tag));
                }
            }
        });

        AppCompatTextView tvCombination = binding.label2;
        tvCombination.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rvCombination.getVisibility() == View.GONE) {
                    rvCombination.setVisibility(View.VISIBLE);
                } else {
                    rvCombination.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initTags() {
        for (StockToolEnums ste : StockToolEnums.values()) {
            optionalTags.add(ste.getName());
        }
    }
}
